<!DOCTYPE html>
<html lang="en">

<head>
    <meta charset="UTF-8">
    <meta http-equiv="X-UA-Compatible" content="IE=edge">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>Document</title>
    <style>
        ::-webkit-scrollbar {
            display: none
        }
    </style>
</head>

<body style="margin: 0;">
    <div style="width: 100%; height: 100vh;">
        <video id="myVideo" controls autoplay style="width: 100%; height: 100%;">
            <!-- <source src="file:///C:/weijun.wu/video/第九套广播体操.mp4" type="video/mp4"> -->
            <!-- <source src="movie.ogg" type="video/ogg">
            <source src="movie.webm" type="video/webm"> -->
        </video>
    </div>

    <script>
        let myVideo = document.getElementById("myVideo");
        let timeout_;

        myVideo.src = getSearchToJson(window.location.href).video;

        myVideo.addEventListener('loadeddata', (event) => {
            console.log(myVideo.duration);   // 视频总时长
            timeout_ = setTimeout(() => {
                window.close();
            }, myVideo.duration * 1000);
        });

        myVideo.addEventListener('play', (event) => {
            // console.log('play');
            clearTimeout(timeout_);
            timeout_ = setTimeout(() => {
                window.close();
            }, myVideo.duration * 1000);
        });

        myVideo.addEventListener('ended', (event) => {
            // console.log('ended');
            window.close();
        });

        // 向父页面发送信息
        function myPostMessage(msg) {
            window.opener && window.opener.postMessage({
                exe: msg,
                url: getSearchToJson(window.location.href)
            }, "*")
        }
        window.onunload = () => {   // 监听页面关闭
            myPostMessage(true)
        }

        /**
         * 将url解析成json队形
         * @url 如果有不是从location对象中获取的url，可以使用url参数传入url的字符串。
         * 返回一个json对象：
         *  url_：基础的请求地址，没有参数。
         *  其他参数，以键值对形式。
         **/
        function getSearchToJson(url) {
            var obj = {}
            var urlAll = url ? url : decodeURIComponent(location.search);
            var urlArr = urlAll.split("?")
            obj['url_'] = urlArr[0];
            if (urlArr.length !== 2) return obj;

            var searchArr = urlArr[1].split("&");
            for (let index = 0; index < searchArr.length; index++) {
                var s_ = searchArr[index].split("=");
                obj[s_[0]] = s_[1]
            }
            return obj;
        }
    </script>
</body>

</html>